<template>
  <div class="app-container" style="display: flex">
    <div class="detail-form">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="auto"
        class="demo-ruleForm"
      >
        <div class="formSty">
          <el-form-item label="企业名称" prop="companyName">
            <el-input v-model="ruleForm.companyName"></el-input>
          </el-form-item>
          <el-form-item label="所在地" prop="companyAddress">
            <el-input v-model="ruleForm.companyAddress"></el-input>
          </el-form-item>
          <el-form-item label="统一社会信用代码" prop="companyIdentifier">
            <el-input v-model="ruleForm.companyIdentifier"></el-input>
          </el-form-item>
          <el-form-item label="注册资金（万元）" prop="registerFund">
            <el-input v-model="ruleForm.registerFund" type="number"></el-input>
          </el-form-item>
        </div>
        <div class="formSty">
          <el-form-item label="法定代表人" prop="lawPerson">
            <el-input v-model="ruleForm.lawPerson"></el-input>
          </el-form-item>
          <el-form-item label="法定代表人手机号码" prop="lawPhone">
            <el-input v-model="ruleForm.lawPhone"></el-input>
          </el-form-item>
          <el-form-item label="企业性质" prop="companyProperty">
            <el-input v-model="ruleForm.companyProperty"></el-input>
          </el-form-item>
          <el-form-item label="所属行业" prop="companyIndustry">
            <el-input v-model="ruleForm.companyIndustry"></el-input>
          </el-form-item>
        </div>
        <div class="formSty">
          <el-form-item label="联系人" prop="contactName">
            <el-input v-model="ruleForm.contactName"></el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="contactPhone">
            <el-input v-model="ruleForm.contactPhone"></el-input>
          </el-form-item>
          <el-form-item label="企业总资产（万元）" prop="totalAssets">
            <el-input v-model="ruleForm.totalAssets" type="number"></el-input>
          </el-form-item>
          <el-form-item label="职工人数（人）" prop="staffNum">
            <el-input v-model="ruleForm.staffNum" type="number"></el-input>
          </el-form-item>
        </div>
        <div class="formSty">
          <el-form-item label="商标总数" prop="brandNum">
            <el-input v-model="ruleForm.brandNum" type="number"></el-input>
          </el-form-item>
          <el-form-item label="知识产权总数" prop="ipNum">
            <el-input v-model="ruleForm.ipNum" type="number"></el-input>
          </el-form-item>
          <el-form-item label="发明专利总数" prop="patentNum">
            <el-input v-model="ruleForm.patentNum" type="number"></el-input>
          </el-form-item>
          <el-form-item label="软件著作权总数" prop="scNum">
            <el-input v-model="ruleForm.scNum" type="number"></el-input>
          </el-form-item>
        </div>
        <div class="formSty">
          <el-form-item label="占地面积（亩）" prop="floorArea">
            <el-input v-model="ruleForm.floorArea" type="number"></el-input>
          </el-form-item>
          <el-form-item label="厂房面积（m²）" prop="plantArea">
            <el-input v-model="ruleForm.plantArea" type="number"></el-input>
          </el-form-item>
        </div>
        <el-form-item label="主要产品" prop="majorProduct" style="width: 50%">
          <el-input v-model="ruleForm.majorProduct" type="textarea"></el-input>
        </el-form-item>

        <!-- 需删除 -->
        <el-form-item label="文件路径">
          <file-upload2 v-model="ruleForm.fileList" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >保存</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import {
  getInfo,
  addInfo,
  addCompanyInfo,
  updateInfo,
} from "@/api/system/info";

import fileUpload2 from "@/components/FileUpload/index2";
export default {
  name: "enterpriseDetails",
  components: {
    fileUpload2,
  },
  data() {
    return {
      deptId: null,
      useFlag: null,
      ruleForm: {
        fileList: "",
        brandNum: "", //商标总数？
        companyAddress: "", //地址
        // "companyId": 0,
        companyIdentifier: "", //社会信用代码
        companyIndustry: "", //所属行业
        // "companyIntro": "string",
        companyName: "", //公司名字
        companyProperty: "", //企业性质
        contactName: "", //联系人
        contactPhone: "", //手机号码
        // "createBy": "string",
        // "createTime": "2023-06-27T12:48:39.285Z",
        // "delFlag": "string",
        deptId: 0,
        // "digitalLevel": "string",
        floorArea: "", //占地面积
        // "honor": "string",
        ipNum: "", //知识产权总数
        lawPerson: "", //法人
        lawPhone: "", //法人电话
        majorProduct: "", //主要产品
        params: {},
        patentNum: "", //专利总数
        plantArea: "", //厂房面积
        // "question": "string",
        registerFund: "", //注册资金
        // "remark": "string",
        scNum: "", //著作权总数
        // "searchValue": "string",
        staffNum: "", //职工人数
        // "technicalProcess": "string",
        totalAssets: "", //企业总资产
        // "updateBy": "string",
        // "updateTime": "2023-06-27T12:48:39.285Z"
      },
      rules: {
        companyName: [
          { required: true, message: "请输入企业名称", trigger: "blur" },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        companyAddress: [
          { required: true, message: "请输入所在地", trigger: "blur" },
        ],
        companyIdentifier: [
          {
            required: true,
            message: "请输入统一社会信用代码",
            trigger: "blur",
          },
        ],
        registerFund: [
          { required: true, message: "请输入注册资金", trigger: "blur" },
        ],
        lawPhone: [
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/,
            message: "请输入正确的手机号码或者座机号",
          },
        ],
        companyProperty: [
          { required: true, message: "请输入企业性质", trigger: "blur" },
        ],
        companyIndustry: [
          { required: true, message: "请输入所属行业", trigger: "blur" },
        ],
        majorProduct: [
          { required: true, message: "请输入主要产品", trigger: "blur" },
        ],
        contactName: [
          { required: true, message: "请输入联系人", trigger: "blur" },
        ],
        contactPhone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern:
              /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
            message: "请输入正确的手机号码",
          },
        ],
        totalAssets: [
          { required: true, message: "请输入企业总资产", trigger: "blur" },
        ],
        staffNum: [
          { required: true, message: "请输入职工人数", trigger: "blur" },
        ],
        ipNum: [
          { required: true, message: "请输入知识产权总数", trigger: "blur" },
        ],
        patentNum: [
          { required: true, message: "请输入专利总数", trigger: "blur" },
        ],
        scNum: [
          { required: true, message: "请输入著作权总数", trigger: "blur" },
        ],
        brandNum: [
          { required: true, message: "请输入商标总数", trigger: "blur" },
        ],
        floorArea: [
          { required: true, message: "请输入占地面积", trigger: "blur" },
        ],
        plantArea: [
          { required: true, message: "请输入厂房面积", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 获取诊断企业id
    getDeptId() {
      this.deptId = this.$route.query.id;
      console.log(this.deptId, "企业id");
      getInfo(this.deptId)
        .then((res) => {
          if (res.data) {
            this.useFlag = 1;
            this.ruleForm = res.data;
          } else {
            this.useFlag = 2;
          }
          console.log(res, "ddddd");
          console.log(1111);
        })
        .catch(() => {
          console.log(2222);
        });
    },
    // 保存基本信息
    submitForm(formName) {
      console.log(this.ruleForm.fileList, "this.ruleForm");

      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          this.ruleForm.deptId = this.$route.query.id;
          if (this.useFlag == 1) {
            //已有数据，调用修改
            updateInfo(this.ruleForm).then((res) => {
              console.log(res, "修改结果");
              this.resertDetailForm();
              this.$router.go(-1);
            });
          } else if (this.useFlag == 2) {
            // 无数据，调用新增
            let data = this.ruleForm;
            addCompanyInfo(data).then((res) => {
              console.log(res, "添加结果");
              this.$router.go(-1);
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 更新获取基本信息表单
    resertDetailForm() {
      getInfo(this.deptId).then((res) => {
        this.ruleForm = res.data;
        this.$message({
          message: "保存成功",
          type: "success",
        });
      });
    },
  },
  created() {
    this.getDeptId();
  },
};
</script>
<style lang="scss" scoped>
.detail-form {
  width: 100%;
  .el-form-item {
    margin-right: 65px;
  }
}
.formSty {
  display: flex;
}
</style>
